<template>
	<!-- 添加居民 -->
	<view class="building-add">
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">上传用户照片</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage">
				<image class="img" v-if="userImage==''" src="../../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="userImage"></image>
			</view>
		</view>
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">上传身份证正面</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage2">
				<image class="img" v-if="userSfUpImg==''" src="../../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="userSfUpImg"></image>
			</view>
		</view>
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">上传身份证反面</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage3">
				<image class="img" v-if="userSfBackImg==''" src="../../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="userSfBackImg"></image>
			</view>
		</view>
		<view class="add-content">
			<view class="add-item">
				<view class="item-left">网格<text>*</text></view>
				<view class="item-right" @click="handleSelect">{{ grid }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">真实姓名<text>*</text></view>
				<view class="item-right"><input v-model="userRealname" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入真实姓名" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">电话号码<text>*</text></view>
				<view class="item-right"><input v-model="userPhone" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入电话号码" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">身份证号<text>*</text></view>
				<view class="item-right"><input v-model="userIdCard" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入身份证号" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">民族<text>*</text></view>
				<view class="item-right" @click="Show=true">{{ userNation_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">婚否<text>*</text></view>
				<view class="item-right" @click="Show2=true">{{ userMarrie_text }}</view>
			</view>
			<view class=""  style="padding: 20rpx; font-size: 27rpx;color: #000000;">居民户籍信息</view>
			<navigator class="add-item" url="../../Public/Domicile" hover-class="none">
				<view class="item-left">户籍<text>*</text></view>
				<view class="item-right">{{ familyAddress }}</view>
			</navigator>
			<view class="add-item">
				<view class="item-left">与户主关系<text>*</text></view>
				<view class="item-right" @click="Show3=true">{{ relationBtFamilyHolder_text }}</view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
		<!-- 民族 -->
		<u-select v-model="Show" :list="dictList" @confirm="confirm"></u-select>
		<!-- 婚姻 -->
		<u-select v-model="Show2" :list="marrieList" @confirm="confirm2"></u-select> 
		<!-- 与户主关系 -->
		<u-select v-model="Show3" :list="familyList" @confirm="confirm3"></u-select> 
		
		<!-- 列选择器 -->
		<u-popup v-model="selectShow" mode="bottom">
			<view class="gridcontent">
				<view class="item">
					<text @click="TCShow=true">通川区</text>
					<view class="item1" v-for="(item,index) in selectList" :key="index" v-if="TCShow">
						<view class="item2" @click="confirmGird(item.depart.departName,item.depart.id)">
							<text @click="TCShow2=true">{{ item.depart.departName }}</text>
							<view class="item3" v-for="(it,i) in item.son" :key="i" v-if="TCShow2" @click="confirmGird(it.depart.departName,it.depart.id)">
								<text @click="TCShow3=true">{{ it.depart.departName }}</text>
								<view class="item4" v-for="(it2,i2) in it.son" :key="i2" v-if="TCShow3" @click="confirmGird(it2.depart.departName,it2.depart.girdId)">
									{{ it2.depart.departName }}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default{
		data() {
			return{
				selectShow:false,
				selectList: [],
				TCShow:false,
				TCShow2:false,
				TCShow3:false,
				grid: '塔沱社区',
				gridId:'', // 网格ID
				dictList:[],
				Show:false,
				Show2:false,
				Show3:false,
				address:'',
				longitude:'',
				latitude:'',
				marrieList:[],
				familyList:[],
				userIdCard:'', // 身份证号
				userImage:'',// 头像
				userMarrie:'',// 婚否
				userMarrie_text:'请选择',
				userNation:'',//民族
				userNation_text:'请选择',
				userRealname:'',//用户真实姓名
				userSfBackImg:'',//身份证背面
				userSfUpImg:'',// 身份证正面
				// 居民户籍信息
				familyId:'',
				relationBtFamilyHolder:'',// 与户主关系
				relationBtFamilyHolder_text:'请选择',
				familyAddress:'请选择',
				userPhone:''
			}
		},
		mounted() {
			this.getGridList()
			this.getnation()
			this.getmarrie()
			this.getfamily()
		},
		onShow() {
			let domicile = uni.getStorageSync('domicile')
			console.log(domicile)
			this.familyAddress = domicile.familyAddress
			this.familyId = domicile.id
		},
		methods:{
			// 获取网格
			getGridList() {
				this.$u.api.getGridList()
					.then(res => {
						console.log(res)
						if (res.code == 200) {
							this.selectList = res.result.son
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取网格失败'
						})
					})
			},
			confirmGird(data,id) {
				this.grid = data
				this.gridId = id
				// console.log(this.gridId)
				// this.selectShow = false
			},
			// 列选择器
			handleSelect() {
				this.selectShow = true
			},
			
			// 民族
			getnation() {
				let params = {
					dictCode:'user_nation_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.dictList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取类型失败'
						})
					})
			},
			confirm(e) {
				this.userNation = e[0].value
				this.userNation_text = e[0].label
			},
			// 婚姻状态
			getmarrie() {
				let params = {
					dictCode:'user_marrie_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.marrieList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取类型失败'
						})
					})
			},
			confirm2(e) {
				this.userMarrie = e[0].value
				this.userMarrie_text = e[0].label
			},
			// 与户主关系
			getfamily() {
				let params = {
					dictCode:'relation_bt_family_holder_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.familyList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取类型失败'
						})
					})
			},
			confirm3(e) {
				this.relationBtFamilyHolder = e[0].value
				this.relationBtFamilyHolder_text = e[0].label
			},
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.userImage = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 上传图片
			chooseImage2() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.userSfUpImg = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 上传图片
			chooseImage3() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.userSfBackImg = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 选择地址
			chooseAddress() {
				let self = this
				uni.chooseLocation({
					success(res) {
						// console.log(res)
						self.address = res.address
						self.longitude = res.longitude
						self.latitude = res.latitude
					},
					fail(err) {
						console.log(err)
						uni.showToast({
							icon:'none',
							title:'获取地址失败'
						})
					}
				})
			},
			// 提交
			handleSubmit() {
				if(this.userImage == '') {
					return this.$refs.uToast.show({
						title: '请上传照片'
					})
				}
				if(this.userSfUpImg == '') {
					return this.$refs.uToast.show({
						title: '请上传身份证照片'
					})
				}
				if(this.userSfBackImg == '') {
					return this.$refs.uToast.show({
						title: '请上传身份证照片'
					})
				}
				if(this.userRealname == '') {
					return this.$refs.uToast.show({
						title: '请填写姓名'
					})
				}
				if(this.userPhone == '') {
					return this.$refs.uToast.show({
						title: '请填写电话'
					})
				}
				if(this.userIdCard == '') {
					return this.$refs.uToast.show({
						title: '请填写身份证号'
					})
				}
				if(this.userMarrie == '') {
					return this.$refs.uToast.show({
						title: '请选择婚姻状况'
					})
				}
				if(this.userNation == '') {
					return this.$refs.uToast.show({
						title: '请选择民族'
					})
				}
				if(this.familyId == '') {
					return this.$refs.uToast.show({
						title: '请选择户籍地址'
					})
				}
				if(this.relationBtFamilyHolder == '') {
					return this.$refs.uToast.show({
						title: '请选择与户主关系'
					})
				}
				let params = {
					gridId:this.gridId,
					userIdCard:this.userIdCard,
					userImage:this.userImage,
					userMarrie:this.userMarrie,
					userNation:this.userNation,
					userPhone:this.userPhone,
					userRealname:this.userRealname,
					userSfBackImg:this.userSfBackImg,
					userSfUpImg:this.userSfUpImg,
					familyId:this.familyId,
					relationBtFamilyHolder:this.relationBtFamilyHolder
				}
				console.log(params)
				uni.removeStorageSync('domicile')
				this.$u.api.addResident(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							uni.showToast({
								title:'添加成功',
								icon:'success'
							})
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:err.message,
							icon:'none'
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.gridcontent{
			height: 40vh;
			width: 100%;
			padding: 20rpx;
			color: #333;
			font-size: 27rpx;
			text-align: left;
			.item{
				width: 100%;
				padding: 5rpx 40rpx;
				overflow-y: auto;
				.item1{
					padding: 5rpx 40rpx;
					.item2{
						padding: 5rpx 40rpx;
						.item3{
							padding: 5rpx 40rpx;
							.item4{
								padding: 5rpx 40rpx;
								
							}
						}
					}
				}
			}
		}
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 110rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 30rpx 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					font-size: 27rpx;
					color: #999;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					flex: 4;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
					text{
						display: inline-block;
						margin-left: 5rpx;
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #CCE6FF;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
